/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-10-12 11:23:29
 * @LastEditTime: 2022-10-24 18:49:24
 */
import {
  SET_SELECT_AT,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  SET_SELECT_AT_ID,
  SET_SELECTED_ITEM,
  DELETE_STANDARDIZED_ITEM,
  GET_TASK,
  ADD_INSPECTION_LIST,
  EDIT_STANDARDIZED,
  SET_ACTIVE_STANDARDIZED,
  UPDATE_STANDARDIZED_LIST,
  DETECT_EXECUTING,
  SET_DETECTION_ITEM_PROCESS
} from '../constants';
import { delItemByIdArray, insertItem, editItemById, updateItemById } from '@/utils/list';

export const StandardizedDetectionReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };
    /**
     * @description: 设置选中的全宗ID
     */
    case SET_SELECT_FOND_ID:
      return {
        ...state,
        selectFondId: action.id
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT_ID: {
      return {
        ...state,
        selectATId: action.id
      };
    }

    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 删除待处置列表项目
     */
    case DELETE_STANDARDIZED_ITEM: {
      return {
        ...state,
        StandardizedList: delItemByIdArray(action.ids, state.StandardizedList)
      };
    }

    /**
     * @description: 更新批次列表
     * @param {*} data websocket消息
     * @param {*} item 修改的条目
     * @return {*}
     */
    case SET_DETECTION_ITEM_PROCESS: {
      const { data, item } = action;
      const { progress, message } = data;
      if (progress !== 100) {
        item.progress = progress;
        item.message = message;
      }
      return {
        ...state,
        StandardizedList: updateItemById(item.id, item, state.StandardizedList)
      };
    }

    /**
     * @description: 添加检测任务
     */
    case ADD_INSPECTION_LIST: {
      return { ...state, StandardizedList: insertItem(action.data, state.StandardizedList) };
    }

    /**
     * @description: 修改检测任务
     */
    case EDIT_STANDARDIZED:
      return {
        ...state,
        StandardizedList: editItemById(action.id, action.data, state.StandardizedList)
      };

    /**
     * @description: 获取任务列表
     */
    case GET_TASK: {
      return { ...state, StandardizedList: action.data };
    }

    /**
     * @description: 设置选中检测任务
     */
    case SET_ACTIVE_STANDARDIZED: {
      return {
        ...state,
        activeStandardized: action.data
      };
    }

    /**
     * @description: 更新批次列表
     * @param {Array} data 批次条目
     * @return {*}
     */
    case UPDATE_STANDARDIZED_LIST: {
      return {
        ...state,
        StandardizedList: updateItemById(action.data.id, action.data, state.StandardizedList)
      };
    }

    case DETECT_EXECUTING: {
      return {
        ...state,
        detectExecuting: action.data
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
